package com.codedancer.campus.common.dataresource;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.codedancer.campus.common.model.han.ZhiHuHotModel;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 韩小韩数据
 */
@Component
@Slf4j
public class HanData {

    @Autowired
    private RestTemplate restTemplate;

    // 知乎热搜榜接口
    private final String ZHI_HU_URL = "https://api.vvhan.com/api/hotlist?type=zhihuHot";

    /**
     * 获取知乎热榜数据
     *
     * @return
     */
    public List<ZhiHuHotModel> getZhiHuHotData() {
        log.info("开始拉取韩小韩知乎热榜数据，接口地址：{}", ZHI_HU_URL);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(ZHI_HU_URL,
            String.class);
        JSONObject jsonObject = JSONUtil.parseObj(responseEntity.getBody());
        if (jsonObject.get("success").toString().equals("true")) {
            List<ZhiHuHotModel> zhiHuHotModels = JSONUtil.toList(jsonObject.get("data").toString(),
                ZhiHuHotModel.class);
            return zhiHuHotModels;
        }
        return null;
    }
}
